// 获取应用实例
const App = getApp();
var intervalID = null;
var timer_status1 = null;
var timer_status = null;

Page({
  data: {
    page_show: false,
    volume: false,
    show: true,

  },


  onShow() {
    //首次打开页面确定当前播放
    let that = this;
    //console.log('首次执行');
    that.getPageData()
  },


  /**
   * 加载页面数据
   */
  getPageData: function () {
    let that = this;
    const myaudio = wx.createInnerAudioContext({});
    that.setData({myaudio:myaudio})
    App._get('audio/index', {}, function (result) {
      that.setData(result.data);
      //console.log(that.data.now_play)
      if (Object.keys(result.data.now_play).length > 0) {
        //console.log('当前需要播放')
        that.setData({
          show: true
        })
        that.queryAnthor(myaudio);
        that.nowTimeBack(myaudio);
        return
      } else if (result.data.now_play.length == 0) {
        //console.log('不需需要播放')
        that.timeBack(myaudio);
        myaudio.offTimeUpdate();
        myaudio.pause();
        myaudio.stop();
        that.setData({
          show: false,
          page_show: true
        })
        return
      }
    });
  },


  //查询是否存在其他直播音乐
  queryAnthor(myaudio) {
    let that = this;
    myaudio.src = that.data.now_play.audio.file_path;
    //myaudio.play(); 
    //console.log('ssssssssssss')
    myaudio.onCanplay(() => {
      //console.log('再次执行了了了了了了了了')
      myaudio.play(); // 安卓手机获取必须在播放状态下才能获取时长
      myaudio.seek(that.data.play_time);
      myaudio.volume=0
      // myaudio.pause(); //立马暂停
      myaudio.duration; //必须初始化，否则获取不到 
      var time = setInterval(() => {
        console.log(myaudio.duration)
        if (myaudio.duration != 0) {
          that.setData({
            duration1: myaudio.duration,
            duration: that.formatTime(myaudio.duration),
          })
          clearTimeout(time)
          myaudio.onTimeUpdate(function () {
            that.setData({
              currentTime: that.formatTime(myaudio.currentTime),
              percent: (myaudio.currentTime / that.data.duration1) * 100
            })
            //console.log(that.data.currentTime)
          })
          if (that.data.play_time > that.data.duration1) {
            myaudio.seek(Number(that.data.duration1));
            that.setData({
              page_show: true
            })
            return
          } else {
            myaudio.offCanplay()
            setTimeout(function () {
              myaudio.play();
              myaudio.volume=1
              that.setData({
                page_show: true
              })
            }, 1000)
          }
        }

      },500);


    })




  },




  //禁音和取消
  changeVolume() {
    let that = this;
    if (that.data.volume) {
      that.data.myaudio.volume = 1
    } else {
      that.data.myaudio.volume = 0
    }
    that.setData({
      volume: !that.data.volume
    })
  },

  //格式化时间
  formatTime(time) {
    var minute = Math.floor(time / 60) % 60;
    var second = Math.floor(time) % 60;
    return (minute < 10 ? '0' + minute : minute) + ':' +
      (second < 10 ? '0' + second : second)
  },

  //监听当前直播结束时间
  nowTimeBack(myaudio) {
    let that = this;
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var end = year + '/' + month + '/' + day + ' ' + that.data.now_play.end_time;
   timer_status1 = setInterval(function () {
      that.$countDown(myaudio,end, 20)
    }, 1000)

  },

  //倒计时下一场直播开始
  timeBack(myaudio) {
    let that = this;
     timer_status = setInterval(function () {
      that.$countDown(myaudio,that.data.next_play, 10)
    }, 1000)

  },

  // 倒计时
  $countDown(myaudio,endTime, type) {
    let that = this;
    var end = new Date(endTime.replace(/-/g, '/'));
    end = end / 1000;
    // 获取当前时间
    let newTime = new Date().getTime() / 1000;
    console.log(type, end, newTime, end - newTime);
    if (end - newTime <= 0) {
      myaudio.stop()
      if (type == 10) {
        clearInterval(timer_status)
        clearTimeout(timer_status);
        if (end - newTime <= 0 && end - newTime >= -5) {
          that.getPageData();
          myaudio.stop();
        }
      } else {
        clearInterval(timer_status1)
        clearTimeout(timer_status1);
        if (end - newTime <= 0 && end - newTime >= -5) {
          that.getPageData();
          myaudio.stop();
        }

      }
    }
  },
  onHide() {
    clearInterval(timer_status1)
    clearTimeout(timer_status1);
    clearInterval(timer_status)
    clearTimeout(timer_status);
   
  },

  onShareAppMessage:function(){

  },

})